/**
 * 文件：PhotoGridAdapter.java
 * 包名：com.ronri.choosephotos;   --- 正式发布时需要修正
 * 说明：照片网格适配器
 * 作者：MS During
 * 日期：2017/5/12
 * 历史：
 */
package com.ronri.choosephotos;

import android.content.Context;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageButton;

import com.cz.nongyetongv5.R;
import com.ronri.common.DensityUtil;

import java.util.List;


public class PhotoGridAdapter extends BaseAdapter {

    private List<PhotoItem> photoGridData = null;

    private LayoutInflater layoutInflater = null;

    private View.OnClickListener clickListener = null;

    private Context context = null;

    public void setPhotoGridData(List<PhotoItem> photoGridData) {
        this.photoGridData = photoGridData;
        this.notifyDataSetChanged();
    }

    /***
     * 构造函数
     * @param context
     * @param photoGridData
     * @param layoutInflater
     * @param clickListener
     */
    public PhotoGridAdapter(Context context, List<PhotoItem> photoGridData,
                            LayoutInflater layoutInflater, View.OnClickListener clickListener) {
        this.photoGridData = photoGridData;
        this.layoutInflater = layoutInflater;

        this.context = context;
        this.clickListener = clickListener;
    }

    @Override
    public int getCount() {
        return photoGridData.size();
    }

    @Override
    public Object getItem(int position) {
        return photoGridData.get(position);
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        ViewGroup iconLayout = null;

        if (convertView != null) {
            iconLayout = (ViewGroup) convertView;
        } else {
            iconLayout = (ViewGroup) layoutInflater.inflate(
                    R.layout.gridview_photo_item, null);
        }

        ImageButton imageButton = (ImageButton) iconLayout
                .findViewById(R.id.ivPhoto);

        imageButton.setOnClickListener(clickListener);

        if (photoGridData.get(position).isOperator) {
            imageButton.setImageResource(R.drawable.camera_add_btn);
            imageButton.setTag("addphoto");
            //iconLayout.findViewById(R.id.ivUploadSure).setVisibility(View.GONE) ;
        } else {
            int widOrHei = DensityUtil.dip2px(150);
            Bitmap bitmap = photoGridData.get(position).GetSmallBitmap(widOrHei, widOrHei);

            if(bitmap != null){
                imageButton.setImageBitmap(bitmap);
                imageButton.setTag(photoGridData.get(position));
            }

            if (photoGridData.get(position).isUpload){
                //iconLayout.findViewById(R.id.ivUploadSure).setVisibility(View.VISIBLE) ;
            }else{
                //iconLayout.findViewById(R.id.ivUploadSure).setVisibility(View.GONE) ;
            }

        }

        return iconLayout;
    }
}
